﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.IO;


using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Drawing.Text;


namespace Cheenti.UserControls
{
    public partial class ShareIt : System.Web.UI.UserControl
    {
        public string ItemIndex
        {
            get;
            set;
        }

        public PostType ShareType
        {
            get;
            set;
        }


        public string RedirectUrl
        {
            get
            {
                switch (this.ShareType)
                {
                    case PostType.FunnyPicture:
                        {
                            return "http://localhost:20202/Pictures.aspx";
                        }

                    case PostType.Joke:
                        {
                            return "http://localhost:20202/Jokes.aspx";
                        }

                    case PostType.Quote:
                        {
                            return "http://localhost:20202/Quotes.aspx";
                        }
                    default:
                        {
                            return "http://localhost:20202/Default.aspx";
                        }
                }


            }
        }

        public string Scope
        {
            get
            {
                return "user_birthday,publish_stream,create_event,user_photos";
            }
        }

        public string AuthenticationUrl
        {
            get
            {
                return string.Empty;
                    //string.Format("https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}&scope={2}", FacebookWebContext.Current.Settings.AppId, HttpUtility.UrlEncode(RedirectUrl), Scope);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                //FacebookLoginHelper helper = new FacebookLoginHelper();
                //if (Request.Params.AllKeys.Contains("code"))
                //{
                //    Dictionary<string, string> dicAccessToken = helper.GetAccessToken(Request["code"].ToString(), Scope, RedirectUrl);
                //    Session.Add("access_token", dicAccessToken["access_token"]);
                //}

                //if (Session["AddOnNextLoad"] != null && Session["AddOnNextLoad"].ToString() == "Yes")
                //{
                //    Session["AddOnNextLoad"] = "No";
                //    this.PostToFacebook();
                //}

            }
        }

        protected void btnShareOnFacebook_Click(object sender, ImageClickEventArgs e)
        {
            //if (Session["access_token"] == null)
            //{
            //    Session.Add("AddOnNextLoad", "Yes");
            //    Response.Redirect(AuthenticationUrl);
            //}
            //else
            //{
            //    PostToFacebook();
            //}
        }

        private void PostToFacebook()
        {

            switch (this.ShareType)
            {
                case PostType.FunnyPicture:
                    {
                        MergeImageAndPostToWall("Pic Message 3");
                        break;
                    }

                case PostType.Joke:
                    {
                        string text = "Pupil (on phone): My son has a bad cold and won't be able to come to school today. \nSchool Secretary: Who is this? \nPupil: This is my father speaking!";
                        PostMessageToWall(text);
                        break;
                    }

                case PostType.Quote:
                    {
                        string text = "Pupil (on phone): My son has a bad cold and won't be able to come to school today. \nSchool Secretary: Who is this? \nPupil: This is my father speaking!";

                        PostMessageToPhotoAlbum("Test Title", text, "Test Footer", text);
                        break;
                    }
            }

            //PostMessageToWall("This is test message for wall");



            //PostImageToPhotoAlbum("Pic Message 2");

            //
        }

        private void MergeImageAndPostToWall(string message)
        {
            //Facebook.FacebookClient cl = new Facebook.FacebookClient(Session["access_token"].ToString());
            //string imagePath = Path.Combine(Server.MapPath("."), @"Images\hall.jpg");

            //System.Drawing.Image playbutton;
            //playbutton = System.Drawing.Image.FromFile(imagePath);

            //string framePath = Path.Combine(Server.MapPath("."), @"Images\TextSlides\text-frame.jpg");
            //System.Drawing.Image frame;
            //frame = System.Drawing.Image.FromFile(framePath);

            //int width = frame.Width, height = frame.Height;
            //using (frame)
            //{
            //    using (var bitmap = new Bitmap(width, height))
            //    {
            //        using (var canvas = Graphics.FromImage(bitmap))
            //        {
            //            canvas.InterpolationMode = InterpolationMode.HighQualityBicubic;
            //            canvas.DrawImage(frame, new Rectangle(0, 0, width, height), new Rectangle(0, 0, frame.Width, frame.Height), GraphicsUnit.Pixel);

            //            int x = 20, y = 20;

            //            //adjust image height
            //            if (playbutton.Height < height - 40)
            //            {

            //            }
            //            else
            //            {
            //            }

            //            //adjust image width
            //            if (playbutton.Width < width - 40)
            //            {
            //            }
            //            else
            //            {
            //            }

            //            canvas.DrawImage(playbutton, x, y);
            //            canvas.Save();
            //        }

            //        MemoryStream outStream = new MemoryStream();
            //        bitmap.Save(outStream, ImageFormat.Jpeg);
            //        Dictionary<string, object> paramss = new Dictionary<string, object>();
            //        paramss.Add("message", message);
            //        FacebookMediaObject pic = new FacebookMediaObject();
            //        pic.ContentType = "image/jpeg";
            //        pic.FileName = "Test.png";
            //        pic.SetValue(outStream.ToArray());
            //        paramss.Add("source", pic);
            //        object res = cl.Post("me/photos", paramss);
            //    }
            //}
        }

        private void PostImageToPhotoAlbum(string message)
        {
            //Facebook.FacebookClient cl = new Facebook.FacebookClient(Session["access_token"].ToString());

            ////Post Picture
            //Dictionary<string, object> paramss = new Dictionary<string, object>();
            //paramss.Add("message", message);
            //FacebookMediaObject pic = new FacebookMediaObject();
            //pic.ContentType = "image/jpeg";
            //pic.FileName = "Test.png";
            //pic.SetValue(File.ReadAllBytes(Path.Combine(Server.MapPath("."), @"Images\05-ant.png")));
            //paramss.Add("source", pic);
            //object res = cl.Post("me/photos", paramss);
        }

        private string PostMessageToPhotoAlbum(string title, string text, string footer, string message)
        {
            return string.Empty;
            ////Post Message as a picture
            //Facebook.FacebookClient cl = new Facebook.FacebookClient(Session["access_token"].ToString());
            //string framePath = Path.Combine(Server.MapPath("."), @"Images\TextSlides\text-frame.jpg");
            //Bitmap bitMapImage = new System.Drawing.Bitmap(framePath);
            //Graphics graphicImage = Graphics.FromImage(bitMapImage);
            //graphicImage.SmoothingMode = SmoothingMode.HighQuality;

            //Color color = Color.FromArgb(255, 89, 145, 0);

            //SolidBrush brush = new SolidBrush(color);

            //graphicImage.DrawString(title, new Font("Arial", 12, FontStyle.Regular), brush, new Point(20, 25));
            //graphicImage.DrawString(text, new Font("Arial", 11, FontStyle.Regular), SystemBrushes.WindowText, new Point(20, 50));
            //graphicImage.DrawString(footer, new Font("Arial", 11, FontStyle.Regular), brush, new Point(20, 390));

            //MemoryStream outStream = new MemoryStream();
            //bitMapImage.Save(outStream, ImageFormat.Jpeg);
            //Dictionary<string, object> paramss = new Dictionary<string, object>();
            //paramss.Add("message", message);
            //FacebookMediaObject pic = new FacebookMediaObject();
            //pic.ContentType = "image/jpeg";
            //pic.FileName = "Test.png";
            //pic.SetValue(outStream.ToArray());
            //paramss.Add("source", pic);
            //object res = cl.Post("me/photos", paramss);
            //graphicImage.Dispose();
            //bitMapImage.Dispose();
            //return framePath;
        }

        private void PostMessageToWall(string message)
        {
            //Facebook.FacebookClient cl = new Facebook.FacebookClient(Session["access_token"].ToString());

            ////Post Message
            //Dictionary<string, string> paramss1 = new Dictionary<string, string>();
            //paramss1.Add("message", message);
            //object res1 = cl.Post("me/feed", paramss1);
        }
    }
}